#
## Makefile for module.
#
CROSS		?= arm-none-eabi-
#CROSS		?= arm-linux-
NAME		:= keyMisc

#
# System environment variable.
#
ifneq (,$(findstring Linux, $(shell uname -s)))
HOSTOS		:= linux
endif
ifneq (,$(findstring windows, $(shell uname -s)))
HOSTOS		:= windows
endif
ifneq (,$(findstring MINGW, $(shell uname -s)))
HOSTOS		:= windows
endif
ifeq ($(strip $(HOSTOS)),)
$(error unable to determine host operation system from uname)
endif

#
## Load variables of flag.
#
ASFLAGS		:= -g -ggdb -Wall -O2 -nostdlib
CFLAGS		:= -g -ggdb -Wall -O2 -nostdlib
CXXFLAGS	:= -g -ggdb -Wall -O2 -nostdlib
#LDFLAGS		:= -T link.ld -nostdlib
LDFLAGS		:= -Ttext 0x0 -nostdlib
ARFLAGS		:= -rcs
OCFLAGS		:= -v -O binary
ODFLAGS		:=
MCFLAGS		:= -mcpu=cortex-a8 -march=armv7-a

LIBDIRS		:=
LIBS 		:=
INCDIRS		:=
SRCDIRS		:=

#
## Add necessary directory for INCDIRS and SRCDIRS.
#
INCDIRS		+= include
SRCDIRS		+= source
OBJDIRS		:= .obj

#
## You shouldn't need to change anything below this point.
#
AS			:= $(CROSS)gcc -x assembler-with-cpp
CC			:= $(CROSS)gcc
CXX			:= $(CROSS)g++
LD			:= $(CROSS)ld
AR			:= $(CROSS)ar
OC			:= $(CROSS)objcopy
OD			:= $(CROSS)objdump
MKDIR		:= mkdir -p
CP			:= cp -af
RM			:= rm -fr
CD			:= cd
FIND		:= find

ifeq ($(strip $(HOSTOS)), linux)
MKHEADER	:= ../tools/make_image/mkv210_image
endif
ifeq ($(strip $(HOSTOS)), windows)
MKHEADER	:= ../tools/windows/mkv210_image
endif

#
# X variables
#
X_ASFLAGS	:= $(MCFLAGS) $(ASFLAGS)
X_CFLAGS	:= $(MCFLAGS) $(CFLAGS)
X_CXXFLAGS	:= $(MCFLAGS) $(CXXFLAGS)
X_LDFLAGS	:= $(LDFLAGS)
X_OCFLAGS	:= $(OCFLAGS)
X_LIBDIRS	:= $(LIBDIRS)
X_LIBS		:= $(LIBS) -lgcc

X_OUT		:= output
X_NAME		:= $(patsubst %, $(X_OUT)/%, $(NAME))
X_INCDIRS	:= $(patsubst %, -I %, $(INCDIRS))
X_SRCDIRS	:= $(patsubst %, %, $(SRCDIRS))
X_OBJDIRS	:= $(patsubst %, $(OBJDIRS)/%, $(X_SRCDIRS))

X_SFILES	:= $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.S))
X_CFILES	:= $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.c))
X_CPPFILES	:= $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.cpp))

X_SDEPS		:= $(patsubst %, $(OBJDIRS)/%, $(X_SFILES:.S=.o.d))
X_CDEPS		:= $(patsubst %, $(OBJDIRS)/%, $(X_CFILES:.c=.o.d))
X_CPPDEPS	:= $(patsubst %, $(OBJDIRS)/%, $(X_CPPFILES:.cpp=.o.d))
X_DEPS		:= $(X_SDEPS) $(X_CDEPS) $(X_CPPDEPS)

X_SOBJS		:= $(patsubst %, $(OBJDIRS)/%, $(X_SFILES:.S=.o))
X_COBJS		:= $(patsubst %, $(OBJDIRS)/%, $(X_CFILES:.c=.o))
X_CPPOBJS	:= $(patsubst %, $(OBJDIRS)/%, $(X_CPPFILES:.cpp=.o)) 
X_OBJS		:= $(X_SOBJS) $(X_COBJS) $(X_CPPOBJS)

VPATH		:= $(X_OBJDIRS)

.PHONY:	all clean
all : $(X_NAME)

$(X_NAME) : $(X_OBJS)
	@echo [LD] Linking $@.elf
	$(CC) $(X_LDFLAGS) $(X_LIBDIRS) -Wl,--cref,-Map=$@.map $^ -o $@.elf $(X_LIBS)
	@echo [OC] Objcopying $@.bin
	$(OC) $(X_OCFLAGS) $@.elf $@.bin
	@echo make header information for irom booting
	$(MKHEADER) $@.bin

$(X_SOBJS) : $(OBJDIRS)/%.o : %.S
	@echo [AS] $<
	@$(AS) $(X_ASFLAGS) $(X_INCDIRS) -c $< -o $@
	@$(AS) $(X_ASFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@

$(X_COBJS) : $(OBJDIRS)/%.o : %.c
	@echo [CC] $<
	@$(CC) $(X_CFLAGS) $(X_INCDIRS) -c $< -o $@
	@$(CC) $(X_CFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@

$(X_CPPOBJS) : $(OBJDIRS)/%.o : %.cpp
	@echo [CXX] $<
	@$(CXX) $(X_CXXFLAGS) $(X_INCDIRS) -c $< -o $@
	@$(CXX) $(X_CXXFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@

clean:
	@$(RM) $(X_DEPS) $(X_OBJS) $(X_OBJDIRS) $(X_OUT) $(OBJDIRS)
	@echo Clean complete.

#
# Include the dependency files, should be place the last of makefile
#
sinclude $(shell $(MKDIR) $(X_OBJDIRS) $(X_OUT)) $(X_DEPS)

test:
	@echo "============================================================"
	@echo [X_ASFLAGS]$(X_ASFLAGS)
	@echo [X_CFLAGS]$(X_CFLAGS)
	@echo [X_CXXFLAGS]$(X_CXXFLAGS)
	@echo [X_LDFLAGS]$(X_LDFLAGS)
	@echo [X_OCFLAGS]$(X_OCFLAGS)
	@echo [X_LIBDIRS]$(X_LIBDIRS)
	@echo [X_LIBS]$(X_LIBS)
	@echo "============================================================"
	@echo [X_SFILES]$(X_SFILES)
	@echo [X_CFILES]$(X_CFILES)
	@echo [X_CPPFILES]$(X_CPPFILES)
	@echo "============================================================"
	@echo [X_SDEPS]$(X_SDEPS)
	@echo [X_CDEPS]$(X_CDEPS)
	@echo [X_CPPDEPS]$(X_CPPDEPS)
	@echo [X_DEPS]$(X_DEPS)
	@echo "============================================================"
	@echo [X_SOBJS]$(X_SOBJS)
	@echo [X_COBJS]$(X_COBJS)
	@echo [X_CPPOBJS]$(X_CPPOBJS)
	@echo [X_OBJS]$(X_OBJS)
	@echo "============================================================"
	@echo [VPATH]$(VPATH)
	@echo [OBJ_DIRS]$(OBJ_DIRS)
	@echo "============================================================"
